package com.xiaoshuidi.cloud.module.iot.service;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.xiaoshuidi.cloud.module.iot.common.exception.HikvisionException;
import com.xiaoshuidi.cloud.module.iot.dal.model.hikvision.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 海康相关接口对接
 *
 * @Author Feng
 * @date 2023/6/26
 */
@Service
public class HikvisionService {

    @Value("${hikvision.host}")
    private String host;

    @Value("${hikvision.app-key}")
    private String appKey;

    @Value("${hikvision.app-secret}")
    private String appSecret;

    private static final String ARTEMIS_PATH = "/artemis";

    /**
     * 单个添加人脸
     *
     * @param dto
     * @return
     */
    public HikvisionCommon<AddFaceRespDto> addFace(AddFaceReqDto dto) {
        try {
            ArtemisConfig config = new ArtemisConfig();
            config.setAppKey(appKey);
            config.setAppSecret(appSecret);
            config.setHost(host);
            String additionDataApi = ARTEMIS_PATH + "/api/frs/v1/face/single/addition";
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("https://", additionDataApi);
                }
            };
            String body = JSONUtil.toJsonStr(dto);
            String result = ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
            HikvisionCommon<AddFaceRespDto> common = JSONUtil.toBean(result, new TypeReference<HikvisionCommon<AddFaceRespDto>>() {
            }, false);
            return common;
        } catch (Exception e) {
            throw new HikvisionException(e);
        }
    }

    /**
     * 单个修改人脸
     *
     * @param dto
     * @return
     */
    public HikvisionCommon<Boolean> updateFace(UpdateFaceReqDto dto) {
        try {
            ArtemisConfig config = new ArtemisConfig();
            config.setAppKey(appKey);
            config.setAppSecret(appSecret);
            config.setHost(host);
            String additionDataApi = ARTEMIS_PATH + "/api/frs/v1/face/single/update";
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("https://", additionDataApi);
                }
            };
            String body = JSONUtil.toJsonStr(dto);
            String result = ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
            HikvisionCommon<Boolean> common = JSONUtil.toBean(result, new TypeReference<HikvisionCommon<Boolean>>() {
            }, false);
            return common;
        } catch (Exception e) {
            throw new HikvisionException(e);
        }
    }


    /**
     * 批量删除人脸
     *
     * @param dto
     * @return
     */
    public HikvisionCommon<Boolean> deletionFace(DeletionFaceReqDto dto) {
        try {
            ArtemisConfig config = new ArtemisConfig();
            config.setAppKey(appKey);
            config.setAppSecret(appSecret);
            config.setHost(host);
            String deletionDataApi = ARTEMIS_PATH + "/api/frs/v1/face/deletion";
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("https://", deletionDataApi);
                }
            };
            String body = JSONUtil.toJsonStr(dto);
            String result = ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
            HikvisionCommon<Boolean> common = JSONUtil.toBean(result, new TypeReference<HikvisionCommon<Boolean>>() {
            }, false);
            return common;
        } catch (Exception e) {
            throw new HikvisionException(e);
        }
    }
}
